<?php
/**
 * 系统角色管理
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.shumenol.com)
 * @version $Id: RoleController.php 1059 2011-11-18 09:26:15Z mmxie $
 */
class System_RoleController extends IG_Controller_Action
{

    /**
     * 角色列表
     * @author maomao.thx@gmail.com
     */
    public function listAction()
    {
        $this->setNoRender();
        $department = new System_Model_Role();
        $report = IG_Data::factory('array', array(
            'source'  => array(
                'name'   => 'Model',
                'config' => array(
                    'model' => 'System_Model_Role',
                ),
            ),
            'plugins' => array(
                array(
                    'name'   => 'DwzTable',
                    'config' => array(
                        'columns' => array(
                            'role_id' => '角色ID',
                            'title'   => 'Title',
                            'name'    => '角色名',
                        ),
                        'attrs' => array('layoutH' => 137),
                    )
                ),
                array(
                    'name'   => 'Params',
                    'config' => array(
                        'name'          => array(
                            'title' => '角色名',
                            'type'  => 'formText',
                            'default'   => '',
                        ),
                    ),
                ),
                array(
                    'name'   => 'DwzPanelBar',
                    'config' => array(
                        'buttons' => array(
                            '添加' => 'class="add" href="/system/role/save/" ' .
                                      'target="navTab" title="添加账号"',
                            '编辑' => 'class="edit" href="/system/role/save/id/{role_id}" ' .
                                      'target="navTab"' .
                                      'title="编辑账号"',
                            '删除' => 'class="delete" ' .
                                      'href="/system/role/delete/id/{role_id}" ' .
                                      'target="ajaxTodo" title="确定要删除吗?" ' .
                                      'callback="IG.data.delete"',
                        ),
                    ),
                ),
            ),
        ));

        $report->loadPlugins();
    }

    /**
     * 角色保存操作
     * @author maomao.thx@gmail.com
     */
    public function saveAction()
    {
        $role = new System_Model_Role();
        if ($this->getRequest()->isPost()) {
            $params = $this->_getParams(array('role_id', 'name', 'title'));
            $this->_checkRole($params['role_id']);
            try {
                $result = $role->setOptions($params)->save();
            } catch (Exception $e) {
                if ($e->getCode() == '23000') {
                    $this->_helper->json(array(
                        'statusCode' => 1,
                        'message'    => "角色名已存在"
                    ));
                } else {
                    throw new IG_Exception($e->getMessage());
                }
            }

            Zend_Registry::get('lvan_cache')->remove('IG_Acl');

            $this->_helper->json(array(
                'statusCode' => (int) ($result === false),
                'message'    => "",
                'callbackType' => 'closeCurrent',
                'navTabId'     => '/system/role/list'
            ));
        }

        if ($id = $this->_getParam('id')) {
            $this->_checkRole();
            $this->view->assign('detail', $role->find($id));
        } else {
            $this->view->assign('detail', $role);
        }
    }

    /**
     * 角色删除
     * @author maomao.thx@gmail.com
     */
    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $id = $this->_getParam('id');
            $this->_checkRole();
            $role = new System_Model_Role();
            $result = $role->delete($id);

            $this->_helper->json(array(
                'statusCode' => (int) ($result === false),
                'message'    => ""
            ));
        }
        exit;
    }

    protected function _checkRole($id = null)
    {
        $id = $this->_getParam('id', $id);
        if (in_array($id, array(1, 2))) {
            if ($this->getRequest()->isPost()) {
                $this->_helper->json(array(
                    'statusCode' => 1,
                    'message'    => '系统角色, 不能删除或编辑',
                    "navTabId" => "",
                    'callbackType' => 'closeCurrent',
                    'forwardUrl' => ''
                ));
            } else {
                echo '<h1>系统角色, 不能删除或编辑</h1>';exit;
            }
        }
    }

    public function assignAction()
    {
        $employeeRole = new System_Model_EmployeeRole();

        switch ($this->_getParam('type')) {
            case 'get':
                $id = $this->_getParam('id');
                echo json_encode(array(
                    'statusCode' => 0, 'data' => $employeeRole->getEmployeeByRole($id)
                ));
                exit;

            case 'set':
                $roleId    = $this->_getParam('id');
                $employees = $this->_getParam('employees', array());
                $employeeRole->saveRoleEmployee($roleId, $employees);
                IG_Log::log("给角色(ID=$roleId)分配用户角色:" . implode(',', $employees));

                Zend_Registry::get('lvan_cache')->remove('IG_Acl');

                echo json_encode(array( 'statusCode' => 0 ));
                exit;
        }
        exit(json_encode(array( 'statusCode' => 1 )));
    }
}